// Example File command script to send sequence of CSI frames
// Uses HActive, VActive settings from the current frame timing settings

# FILE "../CSIDataTypes.txt"

////////////////////////////////////////////////////////////////////////////////////
// Subroutine definitions

# CONST HBLANK = 1000
# CONST VBLANK = 10000

// Send CSI frame using the given VideoPktSub subroutine to generate active line packets
# SUB SendFrame VideoPktSub

	// vertical blanking
	# LP_STATES ACT VBLANK: 3

	// frame start
	# HS_PACKET: DT_FRAME_START 0 0 -1
	
	// active lines
	# Line = 1
	# LOOP_START SYS_VACTIVE
	
		// horizontal blanking
		# LP_STATES ACT HBLANK: 3
	
		// line start
		# HS_PACKET: DT_LINE_START (Line & ffh) ((Line >> 8) & ffh) -1

		// active line
		# CALL VideoPktSub Line
	
		// line end
		# HS_PACKET: DT_LINE_END (Line & ffh) ((Line >> 8) & ffh) -1
		# Line = Line + 1
	
	# LOOP_END

	// frame end
	# HS_PACKET: DT_FRAME_END 0 0 -1
# ENDSUB

// RGB888 packet
# SUB SendPkt_RGB888 Line
	# i = 0
	# HS_PACKET_PLUS_CRC DT_RGB888
	# LOOP_START SYS_HACTIVE
		(i&ffh) ((i+80)&ffh) ((i+160)&ffh)
		# i = i + 1
	# LOOP_END
# ENDSUB

# SUB SendPkt_RGB888_ColorBars Line
	# HS_PACKET_PLUS_CRC DT_RGB888
	# BAR = (7 * LINE - 1) / SYS_VACTIVE
	# IF (BAR == 0)
		*SYS_HACTIVE 255 0 0 
	# ENDIF
	# IF (BAR == 1)
		*SYS_HACTIVE 0 255 0
	# ENDIF
	# IF (BAR == 2)
		*SYS_HACTIVE 0 0 255
	# ENDIF
	# IF (BAR == 3)
		*SYS_HACTIVE 255 255 0
	# ENDIF
	# IF (BAR == 4)
		*SYS_HACTIVE 255 0 255
	# ENDIF
	# IF (BAR == 5)
		*SYS_HACTIVE 0 255 255
	# ENDIF
	# IF (BAR == 6)
		# i = 0
		# BARW = SYS_HACTIVE / 6
		# LAST_BARW = SYS_HACTIVE - (5 * BARW)
		*BARW 255 0 0 
		*BARW 0 255 0
		*BARW 0 0 255
		*BARW 255 255 0
		*BARW 255 0 255
		*LAST_BARW 0 255 255
	# ENDIF
# ENDSUB
	
// RAW8 packet (alternate RG / GB lines)
# SUB SendPkt_Raw8 Line
	# i = Line
	# HS_PACKET_PLUS_CRC DT_RAW8
	# LOOP_START (SYS_HACTIVE / 2)
		# IF (Line & 1)
			(i & ffh) ((255-i) & ffh)
		# ELSE
			((255-i) & ffh) (i & ffh)
		# ENDIF
		# i = i + 1
	# LOOP_END
# ENDSUB

// Assumes frame is stored in buffer FrameBuf and has data type FrameBufDataType
// Assume FrameBufBitsPerPixel is set
# SUB SendPktFromBuffer Line
	# HS_PACKET_PLUS_CRC FrameBufDataType
	# LineLen = FrameBufBitsPerPixel * SYS_HACTIVE / 8
	# STREAM FrameBuf ((Line-1)*LineLen) LineLen
# ENDSUB

////////////////////////////////////////////////////////////////////////////////////
// send frame sequence

# CALL SendFrame "SendPkt_RGB888"
# CALL SendFrame "SendPkt_RGB888_ColorBars"
# CALL SendFrame "SendPkt_Raw8"
	
// load BMP as Raw8 into buffer (rescaling if necessary)
# FrameBufDataType = DT_RAW8
# FrameBufBitsPerPixel = 8
# LOAD_FRAME "../colors1280x720.bmp" FrameBuf FrameBufDataType SYS_HACTIVE SYS_VACTIVE
# CALL SendFrame "SendPktFromBuffer" 

// build Ramp_3 test pattern frame (RGB565 format) in FrameBuf
# FrameBufDataType = DT_RGB565
# FrameBufBitsPerPixel = 16
# LOAD_FRAME "RAMP_3" FrameBuf FrameBufDataType
# CALL SendFrame "SendPktFromBuffer" 

